package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.pojo.*;
import com.itheima.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @title: OrdersServiceImpl
 * @Author 椎名真白
 * @Date: 2022-05-02 15:48
 * @Version 1.0
 */

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrderDetailService orderDetailService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void submit(Orders orders) {
        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        //获取前端传过来的地址ID
        Long addressBookId = orders.getAddressBookId();
        //订单的总金额
        BigDecimal total = new BigDecimal(0);
        //手动获取订单的id
        long ordersID = IdWorker.getId();
        //获取当前登录的用户对象
        User user = userService.getById(userId);
        //获取用户所选择的地址对象
        AddressBook addressBook = addressBookService.getById(addressBookId);

        QueryWrapper<ShoppingCart> queryWrapper=new QueryWrapper<>();
        //获取购物车对象
        queryWrapper.eq("user_id",userId);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);
        //获取订单详情表的数据 并进行封装
        ArrayList<OrderDetail> orderDetailList= new ArrayList<>();
        for (ShoppingCart cart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(ordersID);
            orderDetail.setNumber(cart.getNumber());
            orderDetail.setDishFlavor(cart.getDishFlavor());
            orderDetail.setDishId(cart.getDishId());
            orderDetail.setSetmealId(cart.getSetmealId());
            orderDetail.setName(cart.getName());
            orderDetail.setImage(cart.getImage());
            orderDetail.setAmount(cart.getAmount().multiply(new BigDecimal(cart.getNumber())));
            total=total.add(orderDetail.getAmount());
            orderDetailList.add(orderDetail);
        }
        //添加订单表的数据
        orders.setId(ordersID);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setStatus(2);
        orders.setAmount(total);
        orders.setUserId(userId);
        orders.setNumber("o_"+System.currentTimeMillis());
        orders.setUserName(user.getName());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setConsignee(addressBook.getConsignee());
        this.save(orders);

        orderDetailService.saveBatch(orderDetailList);
        shoppingCartService.remove(queryWrapper);
    }
}
